/**
 * Copyright 2015 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2015年9月10日 下午3:27:44
 */
package com.absir.appserv.system.service;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

import com.absir.appserv.configure.JConfigureUtils;
import com.absir.appserv.system.bean.JPayTrade;
import com.absir.appserv.system.configure.JAlipayConfigure;
import com.absir.bean.core.BeanFactoryUtils;
import com.absir.bean.inject.value.Bean;
import com.alipay.sign.Base64;
import com.alipay.util.AlipaySubmit;

/**
 * @author absir
 *
 */
@Bean
public class AlipayService implements IPayInterface {

	/** ME */
	public static final AlipayService ME = BeanFactoryUtils.get(AlipayService.class);

	/** PLAT_FORM_NAME */
	public static final String PLAT_FORM_NAME = "Alipay";

	/** ALIPAY_CONFIGURE */
	public static final JAlipayConfigure ALIPAY_CONFIGURE = JConfigureUtils.getConfigure(JAlipayConfigure.class);

	/** ALIPAY_GATEWAY_URL */
	public static final String ALIPAY_GATEWAY_URL = "https://openapi.alipay.com/gateway.do";

	private static final String ALGORITHM = "RSA";

	private static final String SIGN_ALGORITHMS = "SHA1WithRSA";

	private static final String DEFAULT_CHARSET = "UTF-8";

	/**
	 * @param content
	 * @param privateKey
	 * @return
	 */
	public static String sign(String content, String privateKey) {
		try {
			PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decode(privateKey));
			KeyFactory keyf = KeyFactory.getInstance(ALGORITHM);
			PrivateKey priKey = keyf.generatePrivate(priPKCS8);

			java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS);
			signature.initSign(priKey);
			signature.update(content.getBytes(DEFAULT_CHARSET));
			byte[] signed = signature.sign();
			return Base64.encode(signed);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * @author absir
	 *
	 */
	public static class PayStatus {

		/** alipay_trade_query_response */
		public PayStatusQuery alipay_trade_query_response;
	}

	/**
	 * @author absir
	 *
	 */
	public static class PayStatusQuery {

		/** trade_no */
		public String trade_no;

		/** trade_status */
		public String trade_status;

		/** total_amount */
		public float total_amount;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.absir.appserv.system.service.IPayInterface#validator(com.absir.appserv
	 * .system.bean.JPayTrade)
	 */
	@Override
	public boolean validator(JPayTrade payTrade) throws Exception {
		Map<String, String> sParaTemp = new HashMap<String, String>();
		sParaTemp.put("service", "single_trade_query");
		sParaTemp.put("partner", ALIPAY_CONFIGURE.getPartnerId());
		sParaTemp.put("_input_charset", AlipaySubmit.input_charset);
		if (payTrade.getTradeNo() != null) {
			sParaTemp.put("trade_no", payTrade.getTradeNo());
		}

		sParaTemp.put("out_trade_no", payTrade.getId());

		// 建立请求
		String sHtmlText = AlipaySubmit.buildRequest("", "", sParaTemp);
		System.out.println(sHtmlText);
		return false;
	}
}
